package org.smart4j.chapter1.plugin.security;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smart4j.chapter1.plugin.security.exception.AuthcException;

/**
 * @ClassName:
 * @Description: Security助手类
 * @Author: LynnZou
 * @Date: 2017/5/31
 */
public final class SecurityHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityHelper.class);

    /**
     * 登陆
     * @param username
     * @param password
     */
    public static void login(String username,String password) throws AuthcException {
        Subject currentUser = SecurityUtils.getSubject();
        if(currentUser != null){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            try{
                currentUser.login(token);
            }catch (AuthenticationException e){
                LOGGER.error("login failure",e);
                throw new AuthcException(e);
            }
        }
    }

    /**
     * 注销
     */
    public static void logout(){

        Subject curretnUser = SecurityUtils.getSubject();
        if(curretnUser != null){
            curretnUser.logout();
        }

    }
}
